﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AnguaRPGEngine.Library.Interfaces;
using AnguaRPGEngine.Library.Parts;
using AnguaRPGEngine.Library.Systems;
using log4net;
using System.Reflection;
using AnguaRPGEngine.Library.Forms;

namespace AnguaRPGEngine.PartsEditor
{
    public partial class PartsEditorForm : Form
    {
        private static readonly ILog _Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        private List<Floor> _Floors;
        private List<Door> _Doors;
        private List<Scenery> _Scenery;
        private List<Wall> _Walls;
        private List<Effect> _Effects;
        private List<Markup> _Markups;
        private bool _EditMode = false;
        private string _LastImageName;




        #region ctor
        public PartsEditorForm()
        {
            InitializeComponent();
            string MethodName = MethodBase.GetCurrentMethod().Name;
            try
            {
                _Log.DebugFormat("{0} - Entry", MethodName);
                _Floors = Serializer<List<Floor>>.DeserializeFromFile(GameSystem.Instance.PathToData + "Images\\Parts\\Floor\\Floors.dat");
                if (_Floors == null)
                {
                    _Floors = new List<Floor>();
                }
                _Doors = Serializer<List<Door>>.DeserializeFromFile(GameSystem.Instance.PathToData + "Images\\Parts\\Door\\Doors.dat");
                if (_Doors == null)
                {
                    _Doors = new List<Door>();
                }
                _Scenery = Serializer<List<Scenery>>.DeserializeFromFile(GameSystem.Instance.PathToData + "Images\\Parts\\Scenery\\Scenery.dat");
                if (_Scenery == null)
                {
                    _Scenery = new List<Scenery>();
                }
                _Walls = Serializer<List<Wall>>.DeserializeFromFile(GameSystem.Instance.PathToData + "Images\\Parts\\Wall\\Walls.dat");
                if (_Walls == null)
                {
                    _Walls = new List<Wall>();
                }
                _Effects = Serializer<List<Effect>>.DeserializeFromFile(GameSystem.Instance.PathToData + "Images\\Parts\\Effect\\Effects.dat");
                if (_Effects == null)
                {
                    _Effects = new List<Effect>();
                }
                _Markups = Serializer<List<Markup>>.DeserializeFromFile(GameSystem.Instance.PathToData + "Images\\Parts\\Markup\\Markups.dat");
                if (_Markups == null)
                {
                    _Markups = new List<Markup>();
                }
            }
            catch (Exception err)
            {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }
        }
        #endregion
        #region Load Editor
        private void LoadDoorEditor()
        {
            string MethodName = MethodBase.GetCurrentMethod().Name;
            try
            {
                _Log.DebugFormat("{0} - Entry", MethodName);
                HideDoorEditor();
                LoadDoorList();
                btnEditDoor.Enabled = btnDeleteDoor.Enabled = false;
            }
            catch (Exception err)
            {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }
        }
        private void LoadFloorEditor()
        {
            string MethodName = MethodBase.GetCurrentMethod().Name;
            try
            {
                _Log.DebugFormat("{0} - Entry", MethodName);
                HideFloorEditor();
                LoadFloorList();
                btnEditFloor.Enabled = btnDeleteFloor.Enabled = false;
            }
            catch (Exception err)
            {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }
        }
        private void LoadSceneryEditor()
        {
            string MethodName = MethodBase.GetCurrentMethod().Name;
            try
            {
                _Log.DebugFormat("{0} - Entry", MethodName);
                HideSceneryEditor();
                LoadSceneryList();
                btnEditScenery.Enabled = btnDeleteScenery.Enabled = false;
            }
            catch (Exception err)
            {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }
        }
        private void LoadWallEditor()
        {
            string MethodName = MethodBase.GetCurrentMethod().Name;
            try
            {
                _Log.DebugFormat("{0} - Entry", MethodName);
                HideWallEditor();
                LoadWallList();
                btnEditWall.Enabled = btnDeleteWall.Enabled = false;
            }
            catch (Exception err)
            {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }
        }
        private void LoadEffectEditor()
        {
            string MethodName = MethodBase.GetCurrentMethod().Name;
            try
            {
                _Log.DebugFormat("{0} - Entry", MethodName);
                HideEffectEditor();
                LoadEffectList();
                btnEditEffect.Enabled = btnDeleteEffect.Enabled = false;
            }
            catch (Exception err)
            {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }
        }
        private void LoadMarkupEditor()
        {
            string MethodName = MethodBase.GetCurrentMethod().Name;
            try
            {
                _Log.DebugFormat("{0} - Entry", MethodName);
                HideMarkupEditor();
                LoadMarkupList();
                btnEditMarkup.Enabled = btnDeleteMarkup.Enabled = false;
            }
            catch (Exception err)
            {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }
        }
        #endregion
        #region Load List
        private void LoadDoorList()
        {
            string MethodName = MethodBase.GetCurrentMethod().Name;
           
            try
            {
                _Log.DebugFormat("{0} - Entry", MethodName);
                lvDoors.Items.Clear();
                foreach (Door door in _Doors)
                {
                    lvDoors.Items.Add(door.Name);
                }
            }
            catch (Exception err)
            {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }
        }
        private void LoadFloorList()
        {
            string MethodName = MethodBase.GetCurrentMethod().Name;
            try
            {
                _Log.DebugFormat("{0} - Entry", MethodName);
                lvFloors.Items.Clear();
                foreach (Floor floor in _Floors)
                {
                    lvFloors.Items.Add(floor.Name);
                }
            }
            catch (Exception err)
            {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }
        }
        private void LoadSceneryList()
        {
            string MethodName = MethodBase.GetCurrentMethod().Name;
            try
            {
                _Log.DebugFormat("{0} - Entry", MethodName);
                lvScenery.Items.Clear();
                foreach (Scenery scenery in _Scenery)
                {
                    lvScenery.Items.Add(scenery.Name);
                }
            }
            catch (Exception err)
            {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }
        }
        private void LoadWallList()
        {
            string MethodName = MethodBase.GetCurrentMethod().Name;
            try
            {
                _Log.DebugFormat("{0} - Entry", MethodName);
                lvWalls.Items.Clear();
                foreach (Wall wall in _Walls)
                {
                    lvWalls.Items.Add(wall.Name);
                }
            }
            catch (Exception err)
            {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }
        }
        private void LoadEffectList()
        {
            string MethodName = MethodBase.GetCurrentMethod().Name;
            try
            {
                _Log.DebugFormat("{0} - Entry", MethodName);
                lvEffects.Items.Clear();
                foreach (Effect effect in _Effects)
                {
                    lvEffects.Items.Add(effect.Name);
                    
                }
            }
            catch (Exception err)
            {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }
        }
        private void LoadMarkupList()
        {
            string MethodName = MethodBase.GetCurrentMethod().Name;
            try
            {
                _Log.DebugFormat("{0} - Entry", MethodName);
                lvMarkups.Items.Clear();
                foreach (Markup markup in _Markups)
                {
                    lvMarkups.Items.Add(markup.Name);
                }
            }
            catch (Exception err)
            {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }
        }
        #endregion
        #region Hide Editor
        private void HideDoorEditor()
        {
            string MethodName = MethodBase.GetCurrentMethod().Name;
            try
            {
                _Log.DebugFormat("{0} - Entry", MethodName);
                lvDoors.Enabled = pnlDoorCommands.Enabled = true;
                lblOpenImage.Visible = lblDoorKeywords.Visible = lblDoorName.Visible = lblDoorClosedSize.Visible = lblClosedImage.Visible =
                    lblDoorOpenedSize.Visible = txtDoorOpenedSize.Visible = txtDoorOpenedImageName.Visible = txtDoorKeywords.Visible =
                    txtDoorName.Visible = txtDoorClosedSize.Visible = lblDoorCloseImageName.Visible = lblDoorOpenedImageName.Visible =
                    btnCancelDoor.Visible = btnSaveDoor.Visible = btnSelectClosedImage.Visible = picClosedDoor.Visible =
                    picOpenDoor.Visible = txtDoorClosedImageName.Visible = btnSelectOpenedImage.Visible = false;
            }
            catch (Exception err)
            {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }
        }
        private void HideFloorEditor()
        {
            string MethodName = MethodBase.GetCurrentMethod().Name;
            try
            {
                _Log.DebugFormat("{0} - Entry", MethodName);
                lvFloors.Enabled = pnlFloorCommands.Enabled = true;
                lblFloorImageName.Visible = lblFloorKeywords.Visible = lblFloorName.Visible = lblFloorSize.Visible =
                    txtFloorImageName.Visible = txtFloorKeywords.Visible = txtFloorName.Visible = txtFloorSize.Visible =
                    lblFloorTraversable.Visible = cboFloorTraversable.Visible = picFloor.Visible = btnSelectFloorImage.Visible =
                    btnSaveFloor.Visible = btnCancelFloor.Visible = false;
            }
            catch (Exception err)
            {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }
        }
        private void HideSceneryEditor()
        {
            string MethodName = MethodBase.GetCurrentMethod().Name;
            try
            {
                _Log.DebugFormat("{0} - Entry", MethodName);
                lvScenery.Enabled = pnlSceneryCommands.Enabled = true;
                lblSceneryImageName.Visible = lblSceneryKeywords.Visible = lblSceneryName.Visible = lblScenerySize.Visible =
                    txtSceneryImageName.Visible = txtSceneryKeywords.Visible = txtSceneryName.Visible = txtScenerySize.Visible =
                    picScenery.Visible = btnSelectSceneryImage.Visible = btnCancelScenery.Visible = btnSaveScenery.Visible = false;
            }
            catch (Exception err)
            {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }
        }
        private void HideWallEditor()
        {
            string MethodName = MethodBase.GetCurrentMethod().Name;
            try
            {
                _Log.DebugFormat("{0} - Entry", MethodName);
                lvWalls.Enabled = pnlWallCommands.Enabled = true;
                lblWallName.Visible = txtWallName.Visible = lblWallKeywords.Visible = txtWallKeywords.Visible = lblWallImageName.Visible =
                    txtWallImageName.Visible = lblWallSize.Visible = txtWallSize.Visible = picWall.Visible = btnSelectWallImage.Visible =
                    btnCancelWall.Visible = btnSaveWall.Visible = false;
            }
            catch (Exception err)
            {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }
        }
        private void HideEffectEditor()
        {
            string MethodName = MethodBase.GetCurrentMethod().Name;
            try
            {
                _Log.DebugFormat("{0} - Entry", MethodName);
                lvEffects.Enabled = pnlEffectCommands.Enabled = true;
                lblEffectName.Visible = txtEffectName.Visible = lblEffectKeywords.Visible = txtEffectKeywords.Visible = lblEffectImageName.Visible =
                    txtEffectImageName.Visible = lblEffectSize.Visible = txtEffectSize.Visible = picEffect.Visible = btnSelectEffectImage.Visible =
                    btnCancelEffect.Visible = btnSaveEffect.Visible = false;
            }
            catch (Exception err)
            {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }
        }
        private void HideMarkupEditor()
        {
            string MethodName = MethodBase.GetCurrentMethod().Name;
            try
            {
                _Log.DebugFormat("{0} - Entry", MethodName);
                lvMarkups.Enabled = pnlMarkupCommands.Enabled = true;
                lblMarkupName.Visible = txtMarkupName.Visible = lblMarkupKeywords.Visible = txtMarkupKeywords.Visible = lblMarkupImageName.Visible =
                    txtMarkupImageName.Visible = lblMarkupSize.Visible = txtMarkupSize.Visible = picMarkup.Visible = btnSelectMarkupImage.Visible =
                    btnCancelMarkup.Visible = btnSaveMarkup.Visible = false;
            }
            catch (Exception err)
            {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }
        }
        #endregion
        #region Show Editor
        private void ShowDoorEditor()
        {
            string MethodName = MethodBase.GetCurrentMethod().Name;
            try
            {
                _Log.DebugFormat("{0} - Entry", MethodName);
                lvDoors.Enabled = pnlDoorCommands.Enabled = false;
                lblOpenImage.Visible = lblDoorKeywords.Visible = lblDoorName.Visible = lblDoorClosedSize.Visible = lblClosedImage.Visible =
                    lblDoorCloseImageName.Visible = txtDoorOpenedImageName.Visible = txtDoorKeywords.Visible = txtDoorName.Visible =
                    txtDoorClosedSize.Visible = lblDoorOpenedImageName.Visible = picClosedDoor.Visible = picOpenDoor.Visible =
                    txtDoorClosedImageName.Visible = lblDoorOpenedSize.Visible = txtDoorOpenedSize.Visible = btnSelectClosedImage.Visible =
                    btnSelectOpenedImage.Visible = btnCancelDoor.Visible = btnSaveDoor.Visible = true;
                picClosedDoor.Image = picOpenDoor.Image = null;
            }
            catch (Exception err)
            {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }
        }
        private void ShowFloorEditor()
        {
            string MethodName = MethodBase.GetCurrentMethod().Name;
            try
            {
                _Log.DebugFormat("{0} - Entry", MethodName);
                lvFloors.Enabled = pnlFloorCommands.Enabled = false;
                lblFloorImageName.Visible = lblFloorKeywords.Visible = lblFloorName.Visible = lblFloorSize.Visible =
                    txtFloorImageName.Visible = txtFloorKeywords.Visible = txtFloorName.Visible = txtFloorSize.Visible =
                    lblFloorTraversable.Visible = cboFloorTraversable.Visible = picFloor.Visible = btnSelectFloorImage.Visible =
                    btnSaveFloor.Visible = btnCancelFloor.Visible = true;
                picFloor.Image = null;
            }
            catch (Exception err)
            {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }
        }
        private void ShowSceneryEditor()
        {
            string MethodName = MethodBase.GetCurrentMethod().Name;
            try
            {
                _Log.DebugFormat("{0} - Entry", MethodName);
                lvScenery.Enabled = pnlSceneryCommands.Enabled = false;
                lblSceneryImageName.Visible = lblSceneryKeywords.Visible = lblSceneryName.Visible = lblScenerySize.Visible =
                    txtSceneryImageName.Visible = txtSceneryKeywords.Visible = txtSceneryName.Visible = txtScenerySize.Visible =
                    picScenery.Visible = btnSelectSceneryImage.Visible = btnCancelScenery.Visible = btnSaveScenery.Visible = true;
                picScenery.Image = null;
            }
            catch (Exception err)
            {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }
        }
        private void ShowWallEditor()
        {
            string MethodName = MethodBase.GetCurrentMethod().Name;
            try
            {
                _Log.DebugFormat("{0} - Entry", MethodName);
                lvWalls.Enabled = pnlWallCommands.Enabled = false;
                lblWallName.Visible = txtWallName.Visible = lblWallKeywords.Visible = txtWallKeywords.Visible = lblWallImageName.Visible =
                    txtWallImageName.Visible = lblWallSize.Visible = txtWallSize.Visible = picWall.Visible = btnSelectWallImage.Visible =
                    btnCancelWall.Visible = btnSaveWall.Visible = true;
                picWall.Image = null;
            }
            catch (Exception err)
            {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }
        }
        private void ShowEffectEditor()
        {
            string MethodName = MethodBase.GetCurrentMethod().Name;
            try
            {
                _Log.DebugFormat("{0} - Entry", MethodName);
                lvEffects.Enabled = pnlEffectCommands.Enabled = false;
                lblEffectName.Visible = txtEffectName.Visible = lblEffectKeywords.Visible = txtEffectKeywords.Visible = lblEffectImageName.Visible =
                    txtEffectImageName.Visible = lblEffectSize.Visible = txtEffectSize.Visible = picEffect.Visible = btnSelectEffectImage.Visible =
                    btnCancelEffect.Visible = btnSaveEffect.Visible = true;
                picEffect.Image = null;
            }
            catch (Exception err)
            {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }
        }
        private void ShowMarkupEditor()
        {
            string MethodName = MethodBase.GetCurrentMethod().Name;
            try
            {
                _Log.DebugFormat("{0} - Entry", MethodName);
                lvMarkups.Enabled = pnlMarkupCommands.Enabled = false;
                lblMarkupName.Visible = txtMarkupName.Visible = lblMarkupKeywords.Visible = txtMarkupKeywords.Visible = lblMarkupImageName.Visible =
                    txtMarkupImageName.Visible = lblMarkupSize.Visible = txtMarkupSize.Visible = picMarkup.Visible = btnSelectMarkupImage.Visible =
                    btnCancelMarkup.Visible = btnSaveMarkup.Visible = true;
                picMarkup.Image = null;
            }
            catch (Exception err)
            {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }
        }
        #endregion
        #region Editor Form Load Event
        private void PartsEditorForm_Load(object sender, EventArgs e)
        {
            string MethodName = MethodBase.GetCurrentMethod().Name;
            try
            {




                using (SplashScreen ss = new SplashScreen(string.Format("Parts Editor v{0}", Application.ProductVersion)))
                {
                    ss.ShowDialog();
                }
                _Log.DebugFormat("{0} - Entry", MethodName);
                LoadDoorEditor();
                LoadFloorEditor();
                LoadSceneryEditor();
                LoadWallEditor();
                LoadEffectEditor();
                LoadMarkupEditor();
            }
            catch (Exception err)
            {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }
        }
        #endregion
        #region Add Door Click Event
        private void btnAddDoor_Click(object sender, EventArgs e)
        {
            string MethodName = MethodBase.GetCurrentMethod().Name;
            try
            {
                _Log.DebugFormat("{0} - Entry", MethodName);
                pBoxDoorImagePreview.Hide();
                txtDoorClosedImageName.Text =
                             txtDoorKeywords.Text =
                             txtDoorName.Text =
                             txtDoorOpenedImageName.Text =
                             txtDoorClosedSize.Text = "";
                ShowDoorEditor();
            }
            catch (Exception err)
            {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }
        }
        #endregion
        #region Edit Door Click Event
        private void btnEditDoor_Click(object sender, EventArgs e)
        {
            string MethodName = MethodBase.GetCurrentMethod().Name;
            try
            {
                _Log.DebugFormat("{0} - Entry", MethodName);
                _EditMode = true;
                pBoxDoorImagePreview.Hide();
               
                txtDoorClosedImageName.Text = _Doors[lvDoors.SelectedItems[0].Index].ClosedImageName;
                txtDoorClosedSize.Text = string.Format("Width: {0}, Height: {1}", _Doors[lvDoors.SelectedItems[0].Index].Size.Width, _Doors[lvDoors.SelectedItems[0].Index].Size.Height);
                txtDoorKeywords.Text = _Doors[lvDoors.SelectedItems[0].Index].Keywords;
                txtDoorName.Text = _Doors[lvDoors.SelectedItems[0].Index].Name;
                txtDoorOpenedImageName.Text = _Doors[lvDoors.SelectedItems[0].Index].OpenedImageName;
                txtDoorOpenedSize.Text = string.Format("Width: {0}, Height: {1}", _Doors[lvDoors.SelectedItems[0].Index].Size.Width, _Doors[lvDoors.SelectedItems[0].Index].Size.Height);

                _LastImageName = GameSystem.Instance.PathToData + "Images\\" + txtDoorClosedImageName.Text.Replace(".", "\\") + ".png";
                picClosedDoor.Image = Image.FromFile(_LastImageName);
                picClosedDoor.Invalidate();
                _LastImageName = GameSystem.Instance.PathToData + "Images\\" + txtDoorOpenedImageName.Text.Replace(".", "\\") + ".png";
                picOpenDoor.Image = Image.FromFile(_LastImageName);
                ShowDoorEditor();
            }
            catch (Exception err)
            {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }
        }
        #endregion
        #region Delete Door Click Event
        private void btnDeleteDoor_Click(object sender, EventArgs e)
        {
            string MethodName = MethodBase.GetCurrentMethod().Name;
            pBoxDoorImagePreview.Hide();
            try
            {
                _Log.DebugFormat("{0} - Entry", MethodName);
                if (MessageBox.Show(this, string.Format("Are you certain that you wish to delete th{0}?", lvDoors.SelectedItems.Count > 1 ? string.Format("ese {0} Doors", lvDoors.SelectedItems.Count) : "is Door"),
                               string.Format("Confirm{0}Deletion", lvDoors.SelectedItems.Count > 1 ? " Multiple " : " "), MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                {
                    for (int d = lvDoors.SelectedItems.Count - 1; d > -1; d--)
                    {
                        _Doors.RemoveAt(lvDoors.SelectedItems[d].Index);
                    }
                    LoadDoorEditor();
                }
            }
            catch (Exception err)
            {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }
        }
        #endregion
        #region Add Floor Click Event
        private void btnAddFloor_Click(object sender, EventArgs e)
        {
            string MethodName = MethodBase.GetCurrentMethod().Name;
            try
            {
                _Log.DebugFormat("{0} - Entry", MethodName);
                txtFloorImageName.Text =
                               txtFloorKeywords.Text =
                               txtFloorName.Text =
                               txtFloorSize.Text = "";
                _EditMode = false;
                picFloor.Image = null;
                ShowFloorEditor();
            }
            catch (Exception err)
            {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }
        }
        #endregion
        #region Edit Floor Click Event
        private void btnEditFloor_Click(object sender, EventArgs e)
        {
            string MethodName = MethodBase.GetCurrentMethod().Name;
            pBoxFloorImagePreview.Hide();
            try
            {
                _Log.DebugFormat("{0} - Entry", MethodName);
                _EditMode = true;
               
                txtFloorName.Text = _Floors[lvFloors.SelectedItems[0].Index].Name;
                txtFloorKeywords.Text = _Floors[lvFloors.SelectedItems[0].Index].Keywords;
                txtFloorSize.Text = string.Format("Width: {0}, Height: {1}", _Floors[lvFloors.SelectedItems[0].Index].Size.Width, _Floors[lvFloors.SelectedItems[0].Index].Size.Height);
                txtFloorImageName.Text = _Floors[lvFloors.SelectedItems[0].Index].ImageName;
                cboFloorTraversable.Text = _Floors[lvFloors.SelectedItems[0].Index].Traversable.ToString();

                _LastImageName = GameSystem.Instance.PathToData + "Images\\" + txtFloorImageName.Text.Replace(".", "\\") + ".png";
                picFloor.Image = Image.FromFile(_LastImageName);
                ShowFloorEditor();
            }
            catch (Exception err)
            {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }
        }
        #endregion
        #region Delete Floor Click Event
        private void btnDeleteFloor_Click(object sender, EventArgs e)
        {
            string MethodName = MethodBase.GetCurrentMethod().Name;
            pBoxFloorImagePreview.Hide();
            try
            {
                _Log.DebugFormat("{0} - Entry", MethodName);
                if (MessageBox.Show(this, string.Format("Are you certain that you wish to delete th{0}?", lvFloors.SelectedItems.Count > 1 ? string.Format("ese {0} Floors", lvFloors.SelectedItems.Count) : "is Floor"),
                              string.Format("Confirm{0}Deletion", lvFloors.SelectedItems.Count > 1 ? " Multiple " : " "), MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                {
                    for (int d = lvFloors.SelectedItems.Count - 1; d > -1; d--)
                    {
                        _Floors.RemoveAt(lvFloors.SelectedItems[d].Index);
                    }
                    LoadFloorEditor();
                }
            }
            catch (Exception err)
            {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }
        }
        #endregion
        #region Add Scenery Click Event
        private void btnAddScenery_Click(object sender, EventArgs e)
        {
            string MethodName = MethodBase.GetCurrentMethod().Name;
            try
            {
                _Log.DebugFormat("{0} - Entry", MethodName);
                txtSceneryImageName.Text =
                              txtSceneryKeywords.Text =
                              txtSceneryName.Text =
                              txtScenerySize.Text = "";
                picScenery.Image = null;
                _EditMode = false;
                ShowSceneryEditor();
            }
            catch (Exception err)
            {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }
        }
        #endregion
        #region Edit Scenery Click Event
        private void btnEditScenery_Click(object sender, EventArgs e)
        {
            string MethodName = MethodBase.GetCurrentMethod().Name;
            pBoxSceneryImagePreview.Hide();
            try
            {
               
                _Log.DebugFormat("{0} - Entry", MethodName);
                _EditMode = true;
                txtSceneryImageName.Text = _Scenery[lvScenery.SelectedItems[0].Index].ImageName;
                txtSceneryKeywords.Text = _Scenery[lvScenery.SelectedItems[0].Index].Keywords;
                txtSceneryName.Text = _Scenery[lvScenery.SelectedItems[0].Index].Name;
                txtScenerySize.Text = string.Format("Width: {0}, Height: {1}", _Scenery[lvScenery.SelectedItems[0].Index].Size.Width, _Scenery[lvScenery.SelectedItems[0].Index].Size.Height);


                _LastImageName = GameSystem.Instance.PathToData + "Images\\" + txtSceneryImageName.Text.Replace(".", "\\") + ".png";
                picScenery.Image = Image.FromFile(_LastImageName);
                ShowSceneryEditor();
            }
            catch (Exception err)
            {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }
        }
        #endregion
        #region Delete Scenery Click Event
        private void btnDeleteScenery_Click(object sender, EventArgs e)
        {
            string MethodName = MethodBase.GetCurrentMethod().Name;
            pBoxSceneryImagePreview.Hide();
            try
            {
                _Log.DebugFormat("{0} - Entry", MethodName);
                if (MessageBox.Show(this, string.Format("Are you certain that you wish to delete th{0}?", lvScenery.SelectedItems.Count > 1 ? string.Format("ese {0} Scenery Items", lvScenery.SelectedItems.Count) : "is Scenery Item"),
                              string.Format("Confirm{0}Deletion", lvScenery.SelectedItems.Count > 1 ? " Multiple " : " "), MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                {
                    for (int d = lvScenery.SelectedItems.Count - 1; d > -1; d--)
                    {
                        _Scenery.RemoveAt(lvScenery.SelectedItems[d].Index);
                    }
                    LoadSceneryEditor();
                }
            }
            catch (Exception err)
            {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }
        }
        #endregion
        #region Add Wall Click Event
        private void btnAddWall_Click(object sender, EventArgs e)
        {
            string MethodName = MethodBase.GetCurrentMethod().Name;
            try
            {
                _Log.DebugFormat("{0} - Entry", MethodName);
                txtWallImageName.Text =
                             txtWallKeywords.Text =
                             txtWallName.Text =
                             txtWallSize.Text = "";
                ShowWallEditor();
            }
            catch (Exception err)
            {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }
        }
        #endregion
        #region Edit Wall Click Event
        private void btnEditWall_Click(object sender, EventArgs e)
        {
            string MethodName = MethodBase.GetCurrentMethod().Name;
            pBoxWallsImagePreview.Hide();
            try
            {
                _Log.DebugFormat("{0} - Entry", MethodName);
                
                _EditMode = true;
                txtWallImageName.Text = _Walls[lvWalls.SelectedItems[0].Index].ImageName;
                txtWallSize.Text = string.Format("Width: {0}, Height: {1}", _Walls[lvWalls.SelectedItems[0].Index].Size.Width, _Walls[lvWalls.SelectedItems[0].Index].Size.Height);
                txtWallKeywords.Text = _Walls[lvWalls.SelectedItems[0].Index].Keywords;
                txtWallName.Text = _Walls[lvWalls.SelectedItems[0].Index].Name;

                _LastImageName = GameSystem.Instance.PathToData + "Images\\" + txtWallImageName.Text.Replace(".", "\\") + ".png";
                picWall.Image = Image.FromFile(_LastImageName);
                ShowWallEditor();
            }
            catch (Exception err)
            {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }
        }
        #endregion
        #region Delete Wall Click Event
        private void btnDeleteWall_Click(object sender, EventArgs e)
        {
            string MethodName = MethodBase.GetCurrentMethod().Name;
            pBoxWallsImagePreview.Hide();
            try
            {
                _Log.DebugFormat("{0} - Entry", MethodName);
                if (MessageBox.Show(this, string.Format("Are you certain that you wish to delete th{0}?", lvWalls.SelectedItems.Count > 1 ? string.Format("ese {0} Walls", lvWalls.SelectedItems.Count) : "is Wall"),
                               string.Format("Confirm{0}Deletion", lvWalls.SelectedItems.Count > 1 ? " Multiple " : " "), MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                {
                    for (int d = lvWalls.SelectedItems.Count - 1; d > -1; d--)
                    {
                        _Walls.RemoveAt(lvWalls.SelectedItems[d].Index);
                    }
                    LoadWallEditor();
                }
            }
            catch (Exception err)
            {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }
        }
        #endregion
        #region Add Effect Click Event
        private void btnAddEffect_Click(object sender, EventArgs e)
        {
            string MethodName = MethodBase.GetCurrentMethod().Name;
            try
            {
                _Log.DebugFormat("{0} - Entry", MethodName);
                txtEffectImageName.Text =
                             txtEffectKeywords.Text =
                             txtEffectName.Text =
                             txtEffectSize.Text = "";
                ShowEffectEditor();
            }
            catch (Exception err)
            {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }
        }
        #endregion
        #region Edit Effect Click Event
        private void btnEditEffect_Click(object sender, EventArgs e)
        {
            string MethodName = MethodBase.GetCurrentMethod().Name;
            pBoxEffectsImagePreview.Hide();
            try
            {
                _Log.DebugFormat("{0} - Entry", MethodName);
                _EditMode = true;
                txtEffectImageName.Text = _Effects[lvEffects.SelectedItems[0].Index].ImageName;
                txtEffectSize.Text = string.Format("Width: {0}, Height: {1}", _Effects[lvEffects.SelectedItems[0].Index].Size.Width, _Effects[lvEffects.SelectedItems[0].Index].Size.Height);
                txtEffectKeywords.Text = _Effects[lvEffects.SelectedItems[0].Index].Keywords;
                txtEffectName.Text = _Effects[lvEffects.SelectedItems[0].Index].Name;

                _LastImageName = GameSystem.Instance.PathToData + "Images\\" + txtEffectImageName.Text.Replace(".", "\\") + ".png";
                picEffect.Image = Image.FromFile(_LastImageName);
                ShowEffectEditor();
            }
            catch (Exception err)
            {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }
        }
        #endregion
        #region Delete Effect Click Event
        private void btnDeleteEffect_Click(object sender, EventArgs e)
        {
            string MethodName = MethodBase.GetCurrentMethod().Name;
            pBoxEffectsImagePreview.Hide();
            try
            {
                _Log.DebugFormat("{0} - Entry", MethodName);
                if (MessageBox.Show(this, string.Format("Are you certain that you wish to delete th{0}?", lvEffects.SelectedItems.Count > 1 ? string.Format("ese {0} Effects", lvEffects.SelectedItems.Count) : "is Effect"),
                               string.Format("Confirm{0}Deletion", lvEffects.SelectedItems.Count > 1 ? " Multiple " : " "), MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                {
                    for (int d = lvEffects.SelectedItems.Count - 1; d > -1; d--)
                    {
                        _Effects.RemoveAt(lvEffects.SelectedItems[d].Index);
                    }
                    LoadEffectEditor();
                }
            }
            catch (Exception err)
            {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }
        }
        #endregion
        #region Add Markup Click Event
        private void btnAddMarkup_Click(object sender, EventArgs e)
        {
            string MethodName = MethodBase.GetCurrentMethod().Name;
            try
            {
                _Log.DebugFormat("{0} - Entry", MethodName);
                txtMarkupImageName.Text =
                             txtMarkupKeywords.Text =
                             txtMarkupName.Text =
                             txtMarkupSize.Text = "";
                ShowMarkupEditor();
            }
            catch (Exception err)
            {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }
        }
        #endregion
        #region Edit Markup Click Event
        private void btnEditMarkup_Click(object sender, EventArgs e)
        {
            string MethodName = MethodBase.GetCurrentMethod().Name;
            pBoxMarkupsImagePreview.Hide();
            try
            {
                _Log.DebugFormat("{0} - Entry", MethodName);
                _EditMode = true;
                txtMarkupImageName.Text = _Markups[lvMarkups.SelectedItems[0].Index].ImageName;
                txtMarkupSize.Text = string.Format("Width: {0}, Height: {1}", _Markups[lvMarkups.SelectedItems[0].Index].Size.Width, _Markups[lvMarkups.SelectedItems[0].Index].Size.Height);
                txtMarkupKeywords.Text = _Markups[lvMarkups.SelectedItems[0].Index].Keywords;
                txtMarkupName.Text = _Markups[lvMarkups.SelectedItems[0].Index].Name;

                _LastImageName = GameSystem.Instance.PathToData + "Images\\" + txtMarkupImageName.Text.Replace(".", "\\") + ".png";
                picMarkup.Image = Image.FromFile(_LastImageName);
                ShowMarkupEditor();
            }
            catch (Exception err)
            {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }
        }
        #endregion
        #region Delete Markup Click Event
        private void btnDeleteMarkup_Click(object sender, EventArgs e)
        {
            string MethodName = MethodBase.GetCurrentMethod().Name;
            pBoxMarkupsImagePreview.Hide();
            try
            {
                _Log.DebugFormat("{0} - Entry", MethodName);
                if (MessageBox.Show(this, string.Format("Are you certain that you wish to delete th{0}?", lvMarkups.SelectedItems.Count > 1 ? string.Format("ese {0} Markups", lvMarkups.SelectedItems.Count) : "is Markup"),
                               string.Format("Confirm{0}Deletion", lvMarkups.SelectedItems.Count > 1 ? " Multiple " : " "), MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                {
                    for (int d = lvMarkups.SelectedItems.Count - 1; d > -1; d--)
                    {
                        _Markups.RemoveAt(lvMarkups.SelectedItems[d].Index);
                    }
                    LoadMarkupEditor();
                }
            }
            catch (Exception err)
            {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }
        }
        #endregion
        #region Cancel Button Click Events
        #region Cancel Door Click Event
        private void btnCancelDoor_Click(object sender, EventArgs e)
        {
            string MethodName = MethodBase.GetCurrentMethod().Name;
            try
            {
                _Log.DebugFormat("{0} - Entry", MethodName);
                HideDoorEditor();
            }
            catch (Exception err)
            {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }
        }
        #endregion
        #region Cancel Floor Click Event
        private void btnCancelFloor_Click(object sender, EventArgs e)
        {
            string MethodName = MethodBase.GetCurrentMethod().Name;
            try
            {
                _Log.DebugFormat("{0} - Entry", MethodName);
                HideFloorEditor();
            }
            catch (Exception err)
            {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }
        }
        #endregion
        #region Cancel Scenery Click Event
        private void btnCancelScenery_Click(object sender, EventArgs e)
        {
            string MethodName = MethodBase.GetCurrentMethod().Name;
            try
            {
                _Log.DebugFormat("{0} - Entry", MethodName);
                HideSceneryEditor();
            }
            catch (Exception err)
            {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }
        }
        #endregion
        #region Cancel Wall Click Event
        private void btnCancelWall_Click(object sender, EventArgs e)
        {
            string MethodName = MethodBase.GetCurrentMethod().Name;
            try
            {
                _Log.DebugFormat("{0} - Entry", MethodName);
                HideWallEditor();
            }
            catch (Exception err)
            {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }
        }
        #endregion
        #region Cancel Effect Click Event
        private void btnCancelEffect_Click(object sender, EventArgs e)
        {
            string MethodName = MethodBase.GetCurrentMethod().Name;
            try
            {
                _Log.DebugFormat("{0} - Entry", MethodName);
                HideEffectEditor();
            }
            catch (Exception err)
            {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }
        }
        #endregion
        #region Cancel Markup Click Event
        private void btnCancelMarkup_Click(object sender, EventArgs e)
        {
            string MethodName = MethodBase.GetCurrentMethod().Name;
            try
            {
                _Log.DebugFormat("{0} - Entry", MethodName);
                HideMarkupEditor();
            }
            catch (Exception err)
            {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }
        }
        #endregion
        #endregion
        #region Save Button Click Events
        #region Save Door Click Event
        private void btnSaveDoor_Click(object sender, EventArgs e)
        {
            string MethodName = MethodBase.GetCurrentMethod().Name;
            try
            {
                _Log.DebugFormat("{0} - Entry", MethodName);
                Door door = new Door();
                door.OpenedImageName = txtDoorOpenedImageName.Text;
                door.ClosedImageName = txtDoorClosedImageName.Text;
                door.Keywords = txtDoorKeywords.Text;
                door.Name = txtDoorName.Text;
                door.Size = GetRelativeSize(Image.FromFile(_LastImageName));
                door.Closed = true;
                door.VisibleToPlayers = true;
                if (_EditMode)
                {
                    _Doors[lvDoors.SelectedItems[0].Index] = door;
                }
                else
                {
                    _Doors.Add(door);
                }
                LoadDoorEditor();
            }
            catch (Exception err)
            {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }
        }
        #endregion
        #region Save Floor Click Event
        private void btnSaveFloor_Click(object sender, EventArgs e)
        {
            string MethodName = MethodBase.GetCurrentMethod().Name;
            try
            {
                _Log.DebugFormat("{0} - Entry", MethodName);
                Floor floor = new Floor();
                floor.ImageName = txtFloorImageName.Text;
                floor.Keywords = txtFloorKeywords.Text;
                floor.Name = txtFloorName.Text;
                floor.Size = GetRelativeSize(Image.FromFile(_LastImageName));
                floor.Traversable = cboFloorTraversable.Text == "True";
                floor.VisibleToPlayers = true;
                if (_EditMode)
                {
                    _Floors[lvFloors.SelectedItems[0].Index] = floor;
                }
                else
                {
                    _Floors.Add(floor);
                }
                LoadFloorEditor();
            }
            catch (Exception err)
            {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }
        }
        #endregion
        #region Save Scenery Click Event
        private void btnSaveScenery_Click(object sender, EventArgs e)
        {
            string MethodName = MethodBase.GetCurrentMethod().Name;
            try
            {
                _Log.DebugFormat("{0} - Entry", MethodName);
                Scenery scenery = new Scenery();
                scenery.ImageName = txtSceneryImageName.Text;
                scenery.Keywords = txtSceneryKeywords.Text;
                scenery.Name = txtSceneryName.Text;
                scenery.Size = GetRelativeSize(Image.FromFile(_LastImageName));
                scenery.VisibleToPlayers = true;
                if (_EditMode)
                {
                    _Scenery[lvScenery.SelectedItems[0].Index] = scenery;
                }
                else
                {
                    _Scenery.Add(scenery);
                }
                LoadSceneryEditor();
            }
            catch (Exception err)
            {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }
        }
        #endregion
        #region Save Wall Click Event
        private void btnSaveWall_Click(object sender, EventArgs e)
        {
            string MethodName = MethodBase.GetCurrentMethod().Name;
            try
            {
                _Log.DebugFormat("{0} - Entry", MethodName);
                Wall Wall = new Wall();
                Wall.ImageName = txtWallImageName.Text;
                Wall.Keywords = txtWallKeywords.Text;
                Wall.Name = txtWallName.Text;
                Wall.Size = GetRelativeSize(Image.FromFile(_LastImageName));
                Wall.VisibleToPlayers = true;
                if (_EditMode)
                {
                    _Walls[lvWalls.SelectedItems[0].Index] = Wall;
                }
                else
                {
                    _Walls.Add(Wall);
                }
                LoadWallEditor();
            }
            catch (Exception err)
            {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }
        }
        #endregion
        #region Save Effect Click Event
        private void btnSaveEffect_Click(object sender, EventArgs e)
        {
            string MethodName = MethodBase.GetCurrentMethod().Name;
            try
            {
                _Log.DebugFormat("{0} - Entry", MethodName);
                Effect Effect = new Effect();
                Effect.ImageName = txtEffectImageName.Text;
                Effect.Keywords = txtEffectKeywords.Text;
                Effect.Name = txtEffectName.Text;
                Effect.Size = GetRelativeSize(Image.FromFile(_LastImageName));
                Effect.VisibleToPlayers = true;
                if (_EditMode)
                {
                    _Effects[lvEffects.SelectedItems[0].Index] = Effect;
                }
                else
                {
                    _Effects.Add(Effect);
                }
                LoadEffectEditor();
            }
            catch (Exception err)
            {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }
        }
        #endregion
        #region Save Markup Click Event
        private void btnSaveMarkup_Click(object sender, EventArgs e)
        {
            string MethodName = MethodBase.GetCurrentMethod().Name;
            try
            {
                _Log.DebugFormat("{0} - Entry", MethodName);
                Markup Markup = new Markup();
                Markup.ImageName = txtMarkupImageName.Text;
                Markup.Keywords = txtMarkupKeywords.Text;
                Markup.Name = txtMarkupName.Text;
                Markup.Size = GetRelativeSize(Image.FromFile(_LastImageName));
                Markup.VisibleToPlayers = true;
                if (_EditMode)
                {
                    _Markups[lvMarkups.SelectedItems[0].Index] = Markup;
                }
                else
                {
                    _Markups.Add(Markup);
                }
                LoadMarkupEditor();
            }
            catch (Exception err)
            {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }
        }
        #endregion
        #endregion
        #region Get Relative Size Method
        private Size GetRelativeSize(Image image)
        {
            return new Size(image.Width / 50, image.Height / 50);
        }
        #endregion
        #region Get Image Name Method
        private string GetImageName(string fileName)
        {
            string imageName = fileName.Replace(GameSystem.Instance.PathToData + "Images\\", "");
            imageName = imageName.Replace("\\", ".");
            imageName = imageName.Substring(0, imageName.LastIndexOf("."));
            return imageName;
        }
        #endregion

        #region ListView Selected Index Changed Events
        #region Doors
        private void lvDoors_SelectedIndexChanged(object sender, EventArgs e)
        {
            string MethodName = MethodBase.GetCurrentMethod().Name;
          

            try
            {

                _Log.DebugFormat("{0} - Entry", MethodName);
                btnEditDoor.Enabled = btnDeleteDoor.Enabled = lvDoors.SelectedItems.Count > 0;


                _LastImageName = GameSystem.Instance.PathToData + "Images\\" +
                    _Doors[lvDoors.SelectedItems[0].Index].ClosedImageName.Replace(".", "\\") + ".png";

                pBoxDoorImagePreview.Image = Image.FromFile(_LastImageName);
                pBoxDoorImagePreview.Invalidate();
                pBoxDoorImagePreview.MouseHover += delegate
                {
                    ToggleDoorView("open");

                };

                pBoxDoorImagePreview.MouseLeave += delegate
                {

                    ToggleDoorView("Closed");

                };

                pBoxDoorImagePreview.Show();


            }
            catch (Exception err)
            {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }
        }

        /// <summary>
        /// Displays either open or closed door image.
        /// </summary>
        /// <param name="doorStatus">parameter must be open or closed</param>
        private void ToggleDoorView(string doorStatus)
        {
            string MethodName = MethodBase.GetCurrentMethod().Name;
           
            try
           {
               pBoxDoorImagePreview.Image = null;
                  

                _Log.DebugFormat("{0} - Entry", MethodName);

                switch (doorStatus.ToLowerInvariant())
                {
                    case "open":
                        _LastImageName = GameSystem.Instance.PathToData + "Images\\" +
                   _Doors[lvDoors.SelectedItems[0].Index].OpenedImageName.Replace(".", "\\") + ".png";
                        break;
                    case "closed":
                        _LastImageName = GameSystem.Instance.PathToData + "Images\\" +
                   _Doors[lvDoors.SelectedItems[0].Index].ClosedImageName.Replace(".", "\\") + ".png";
                        break;
                    default:
                        break;
                }
               
                pBoxDoorImagePreview.Image = Image.FromFile(_LastImageName);
                pBoxDoorImagePreview.Invalidate();
               
            }
            catch (Exception err)
           {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }

        }




        #endregion
        #region Floors
        private void lvFloors_SelectedIndexChanged(object sender, EventArgs e)
        {
            string MethodName = MethodBase.GetCurrentMethod().Name;
            try
            {
                _Log.DebugFormat("{0} - Entry", MethodName);
                btnEditFloor.Enabled = btnDeleteFloor.Enabled = lvFloors.SelectedItems.Count > 0;


                _LastImageName = GameSystem.Instance.PathToData + "Images\\" +
                    _Floors[lvFloors.SelectedItems[0].Index].ImageName.Replace(".", "\\") + ".png";


                pBoxFloorImagePreview.Image = Image.FromFile(_LastImageName);
                pBoxFloorImagePreview.Invalidate();
                pBoxFloorImagePreview.Show();
                

            }
            catch (Exception err)
            {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }
        }
        #endregion
        #region Scenery
        private void lvScenery_SelectedIndexChanged(object sender, EventArgs e)
        {
            string MethodName = MethodBase.GetCurrentMethod().Name;
            try
            {
                _Log.DebugFormat("{0} - Entry", MethodName);
                btnEditScenery.Enabled = btnDeleteScenery.Enabled = lvScenery.SelectedItems.Count > 0;

                _LastImageName = GameSystem.Instance.PathToData + "Images\\" +
                   _Scenery[lvScenery.SelectedItems[0].Index].ImageName.Replace(".", "\\") + ".png";


                pBoxSceneryImagePreview.Image = Image.FromFile(_LastImageName);
                pBoxSceneryImagePreview.Invalidate();
                pBoxSceneryImagePreview.Show();
               
            }
            catch (Exception err)
            {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }
        }
        #endregion
        #region Walls
        private void lvWalls_SelectedIndexChanged(object sender, EventArgs e)
        {
            MessageBox.Show("inwalls");
            string MethodName = MethodBase.GetCurrentMethod().Name;
            try
            {
                _Log.DebugFormat("{0} - Entry", MethodName);
                btnEditFloor.Enabled = btnDeleteFloor.Enabled = lvWalls.SelectedItems.Count > 0;

                _LastImageName = GameSystem.Instance.PathToData + "Images\\" +
                  _Walls[lvWalls.SelectedItems[0].Index].ImageName.Replace(".", "\\") + ".png";


                pBoxWallsImagePreview.Image = Image.FromFile(_LastImageName);
                pBoxWallsImagePreview.Invalidate();
                pBoxWallsImagePreview.Show();
            }
            catch (Exception err)
            {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }
        }
        #endregion
        #region Effects

        private void lvEffects_SelectedIndexChanged(object sender, EventArgs e)
        {
            string MethodName = MethodBase.GetCurrentMethod().Name;
            try
           {
                _Log.DebugFormat("{0} - Entry", MethodName);
                btnEditEffect.Enabled = btnDeleteEffect.Enabled = lvEffects.SelectedItems.Count > 0;

                _LastImageName = GameSystem.Instance.PathToData + "Images\\" +
                  _Effects[lvEffects.SelectedItems[0].Index].ImageName.Replace(".", "\\") + ".png";


                pBoxEffectsImagePreview.Image = Image.FromFile(_LastImageName);
                pBoxEffectsImagePreview.Invalidate();
                pBoxEffectsImagePreview.Show();
            }
            catch (Exception err)
            {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }
        }
     
        #endregion
        #region Markups

        private void lvMarkups_SelectedIndexChanged(object sender, EventArgs e)
        {
            string MethodName = MethodBase.GetCurrentMethod().Name;
            try
            {
                _Log.DebugFormat("{0} - Entry", MethodName);
                btnEditMarkup.Enabled = btnDeleteMarkup.Enabled = lvMarkups.SelectedItems.Count > 0;

                _LastImageName = GameSystem.Instance.PathToData + "Images\\" +
                  _Markups[lvMarkups.SelectedItems[0].Index].ImageName.Replace(".", "\\") + ".png";


                pBoxMarkupsImagePreview.Image = Image.FromFile(_LastImageName);
                pBoxMarkupsImagePreview.Invalidate();
                pBoxMarkupsImagePreview.Show();
            }
            catch (Exception err)
            {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }
        }
        
        
        
        
        #endregion
        #endregion
        #region Select Image Click Events
        #region Select Floor Image Click Event
        private void btnSelectFloorImage_Click(object sender, EventArgs e)
        {
            string MethodName = MethodBase.GetCurrentMethod().Name;
            try
            {
                _Log.DebugFormat("{0} - Entry", MethodName);
                using (OpenFileDialog ofd = new OpenFileDialog())
                {
                    ofd.Title = "Select Floor Image";
                    ofd.Filter = "PNGs (*.png)|*.png|JPEGs (*.jpg)|*.jpg";
                    if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        Image i = Image.FromFile(ofd.FileName);
                        txtFloorImageName.Text = GetImageName(ofd.FileName);
                        txtFloorSize.Text = string.Format("Width: {0}, Height: {1}", i.Width, i.Height);
                        picFloor.Image = i;
                        _LastImageName = ofd.FileName;
                    }
                }
            }
            catch (Exception err)
            {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }
        }
        #endregion
        #region Select Scenery Image Click Event
        private void btnSelectSceneryImage_Click(object sender, EventArgs e)
        {
            string MethodName = MethodBase.GetCurrentMethod().Name;
            try
            {
                _Log.DebugFormat("{0} - Entry", MethodName);
                using (OpenFileDialog ofd = new OpenFileDialog())
                {
                    ofd.Title = "Select Scenery Image";
                    ofd.Filter = "PNGs (*.png)|*.png|JPEGs (*.jpg)|*.jpg";
                    if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        Image i = Image.FromFile(ofd.FileName);
                        txtSceneryImageName.Text = GetImageName(ofd.FileName);
                        txtScenerySize.Text = string.Format("Width: {0}, Height: {1}", i.Width, i.Height);
                        picScenery.Image = i;
                        _LastImageName = ofd.FileName;
                    }
                }
            }
            catch (Exception err)
            {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }
        }
        #endregion
        #region Select Closed Door Image Click Event
        private void btnSelectClosedImage_Click(object sender, EventArgs e)
        {
            string MethodName = MethodBase.GetCurrentMethod().Name;
            try
            {
                _Log.DebugFormat("{0} - Entry", MethodName);
                using (OpenFileDialog ofd = new OpenFileDialog())
                {
                    ofd.Title = "Select Closed Door Image";
                    ofd.Filter = "PNGs (*.png)|*.png|JPEGs (*.jpg)|*.jpg";
                    if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        Image i = Image.FromFile(ofd.FileName);
                        txtDoorClosedImageName.Text = GetImageName(ofd.FileName);
                        txtDoorClosedSize.Text = string.Format("Width: {0}, Height: {1}", i.Width, i.Height);
                        picClosedDoor.Image = i;
                        _LastImageName = ofd.FileName;
                    }
                }
            }
            catch (Exception err)
            {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }
        }
        #endregion
        #region Select Opened Door Image Click Event
        private void btnSelectOpenedImage_Click(object sender, EventArgs e)
        {
            string MethodName = MethodBase.GetCurrentMethod().Name;
            try
            {
                _Log.DebugFormat("{0} - Entry", MethodName);
                using (OpenFileDialog ofd = new OpenFileDialog())
                {
                    ofd.Title = "Select Opened Door Image";
                    ofd.Filter = "PNGs (*.png)|*.png|JPEGs (*.jpg)|*.jpg";
                    if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        Image i = Image.FromFile(ofd.FileName);
                        txtDoorOpenedImageName.Text = GetImageName(ofd.FileName);
                        txtDoorOpenedSize.Text = string.Format("Width: {0}, Height: {1}", i.Width, i.Height);
                        picOpenDoor.Image = i;
                        _LastImageName = ofd.FileName;
                    }
                }
            }
            catch (Exception err)
            {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }
        }
        #endregion
        #region Select Wall Image Click Event
        private void btnSelectWallImage_Click(object sender, EventArgs e)
        {
            string MethodName = MethodBase.GetCurrentMethod().Name;
            try
            {
                _Log.DebugFormat("{0} - Entry", MethodName);
                using (OpenFileDialog ofd = new OpenFileDialog())
                {
                    ofd.Title = "Select Wall Image";
                    ofd.Filter = "PNGs (*.png)|*.png|JPEGs (*.jpg)|*.jpg";
                    if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        Image i = Image.FromFile(ofd.FileName);
                        txtWallImageName.Text = GetImageName(ofd.FileName);
                        txtWallSize.Text = string.Format("Width: {0}, Height: {1}", i.Width, i.Height);
                        picWall.Image = i;
                        _LastImageName = ofd.FileName;
                    }
                }
            }
            catch (Exception err)
            {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }
        }
        #endregion
        #region Select Effect Image Click Event
        private void btnSelectEffectImage_Click(object sender, EventArgs e)
        {
            string MethodName = MethodBase.GetCurrentMethod().Name;
            try
            {
                _Log.DebugFormat("{0} - Entry", MethodName);
                using (OpenFileDialog ofd = new OpenFileDialog())
                {
                    ofd.Title = "Select Effect Image";
                    ofd.Filter = "PNGs (*.png)|*.png|JPEGs (*.jpg)|*.jpg";
                    if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        Image i = Image.FromFile(ofd.FileName);
                        txtEffectImageName.Text = GetImageName(ofd.FileName);
                        txtEffectSize.Text = string.Format("Width: {0}, Height: {1}", i.Width, i.Height);
                        picEffect.Image = i;
                        _LastImageName = ofd.FileName;
                    }
                }
            }
            catch (Exception err)
            {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }
        }
        #endregion
        #region Select Markup Image Click Event
        private void btnSelectMarkupImage_Click(object sender, EventArgs e)
        {
            string MethodName = MethodBase.GetCurrentMethod().Name;
            try
            {
                _Log.DebugFormat("{0} - Entry", MethodName);
                using (OpenFileDialog ofd = new OpenFileDialog())
                {
                    ofd.Title = "Select Markup Image";
                    ofd.Filter = "PNGs (*.png)|*.png|JPEGs (*.jpg)|*.jpg";
                    if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        Image i = Image.FromFile(ofd.FileName);
                        txtMarkupImageName.Text = GetImageName(ofd.FileName);
                        txtMarkupSize.Text = string.Format("Width: {0}, Height: {1}", i.Width, i.Height);
                        picMarkup.Image = i;
                        _LastImageName = ofd.FileName;
                    }
                }
            }
            catch (Exception err)
            {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }
        }
        #endregion
        #endregion
        #region Editor Form Closing Event
        private void PartsEditorForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            string MethodName = MethodBase.GetCurrentMethod().Name;
            try
            {
                _Log.DebugFormat("{0} - Entry", MethodName);
                Serializer<List<Door>>.SerializeToFile(_Doors, GameSystem.Instance.PathToData + "Images\\Parts\\Door\\Doors.dat");
                Serializer<List<Floor>>.SerializeToFile(_Floors, GameSystem.Instance.PathToData + "Images\\Parts\\Floor\\Floors.dat");
                Serializer<List<Scenery>>.SerializeToFile(_Scenery, GameSystem.Instance.PathToData + "Images\\Parts\\Scenery\\Scenery.dat");
                Serializer<List<Wall>>.SerializeToFile(_Walls, GameSystem.Instance.PathToData + "Images\\Parts\\Wall\\Walls.dat");
                Serializer<List<Effect>>.SerializeToFile(_Effects, GameSystem.Instance.PathToData + "Images\\Parts\\Effect\\Effects.dat");
                Serializer<List<Markup>>.SerializeToFile(_Markups, GameSystem.Instance.PathToData + "Images\\Parts\\Markup\\Markups.dat");
            }
            catch (Exception err)
            {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }
        }
        #endregion


        #region "Load Tab Page Events"
      
        // Ensures that no legacy image remains displayed when selecting the tabs.
        private void tabPage1_Enter(object sender, EventArgs e)
        {
            pBoxDoorImagePreview.Hide();
           
        }

        private void tabPage2_Enter(object sender, EventArgs e)
        {
            pBoxFloorImagePreview.Hide();
            
        }

        private void tabPage3_Enter(object sender, EventArgs e)
        {
            pBoxSceneryImagePreview.Hide();
            
        }

        private void tabPage4_Enter(object sender, EventArgs e)
        {
            pBoxWallsImagePreview.Hide();
        }

        private void tabPage5_Enter(object sender, EventArgs e)
        {
            pBoxEffectsImagePreview.Hide();
        }

        private void tabPage6_Enter(object sender, EventArgs e)
        {
            pBoxMarkupsImagePreview.Hide();
        }

        #endregion

       

       

        

       

        


    }
}